package com.jobs.producer0.controller;

import com.jobs.common.RS;
import com.jobs.common.consts.Constant;
import com.jobs.producer0.config.RabbitmqConfigs;
import com.jobs.producer0.service.DemoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 测试信息
 * @author xxx
 */
@RestController
@RequestMapping("/demo")
@Slf4j
public class DemoController {
    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    private DemoService demoService;
    @Autowired
    private RabbitmqConfigs rabbitmqConfigs;

    @GetMapping("/test")
    public String demo() {
        return RS.success(demoService.demo());
    }

    @GetMapping("/direct")
    public  void direct(@RequestParam  String type, @RequestParam  String message) {
        String exchangeName="maxxub.direct";
        String msg="红色警报";
        //参数：交换机，路由，消息
        rabbitTemplate.convertAndSend(exchangeName,"red",msg);
    }
    @GetMapping("/topic")
    public  void topic(@RequestParam  String type, @RequestParam  String message) {
        String exchangeName="maxxub.topic";
        String msg="中国繁荣";
        rabbitTemplate.convertAndSend(exchangeName,"china.strong",msg);
    }

    @GetMapping("/ttl")
    public  void ttl(@RequestParam  String type, @RequestParam  String message) {
        rabbitTemplate.convertAndSend("simple.direct", "hi", "hello", new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                //设置消息拿到消息参数，进行设置过期时间
                message.getMessageProperties().setExpiration("10000");
                return message;
            }
        });
        //下面打印一下消息是执行成功的
        log.info("消息发送成功");
    }




    @GetMapping("/product")
    public  void product(@RequestParam  String type, @RequestParam  String message) {
        rabbitTemplate.convertAndSend(RabbitmqConfigs.EXCHANGE_TOPICS_INFORM, "inform."+type, message);
    }

    @GetMapping("/type/simple")
    public void testSendMessage2Queue(@RequestParam  String message){
        String queueName="simple.queue";
        //我们这里写队列名称一会在管理页面创建
        String msg="hello,ampq!"+message;
        //消息
        //我们这里写队列名称一会在管理页面创建
        rabbitTemplate.convertAndSend(queueName,msg);
    }

    String msg = "一条用于发布确认的消息";

    @GetMapping("/noExchange")
    public void noExchange() {
        rabbitTemplate.convertAndSend("noExchange", "noExchange", msg);
    }

    @GetMapping("/toExchange")
    public void toExchange() {
        rabbitTemplate.convertAndSend(Constant.CONFIRM_EXCHANGE, "xxx.xxx.xxx", msg);
    }

    @GetMapping("/toQueue")
    public void toQueue() {
        rabbitTemplate.convertAndSend(Constant.CONFIRM_EXCHANGE, Constant.CONFIRM_ROUTING_KEY, msg);
    }

}
